import random
import astar

##################################################
# creat random route in the map
##################################################
class RandomMove:
    def __init__(self):
        self.reset()
        self.turn_pos1 = [] # the 1st turn
        self.turn_pos2 = [] # the 2nd turn
        self.start_pos = [-1, -1] 
        self.status = 0 # the start state of FSM
        self.reset_time = 10

    def reset(self):
        self.map = []
        self.way = []
        self.stop = True
        self.direction = 0

    def produce_turns(self):
        """ produce turn points """
        # produce the 1st turn
        while 1:
            self.turn_pos1 = []
            self.turn_pos1.append(random.randint(0, self.length - 1))
            self.turn_pos1.append(random.randint(0, self.height - 1))
            if self.map[self.turn_pos1[0]][self.turn_pos1[1]] == 0:
                break

        # produce the 2nd turn
        while 1:
            self.turn_pos2 = []
            self.turn_pos2.append(random.randint(0, self.length - 1))
            self.turn_pos2.append(random.randint(0, self.height - 1))
            if self.map[self.turn_pos2[0]][self.turn_pos2[1]] == 0 :
                break

    def adjust_turns(self, start_position):
        """ if blocks happens, adjust the turns """
        if self.start_pos == start_position:
            if self.status == 0: # tanker be blocked when running to the 1st turn
                while 1 :
                    i = random.randint(0, self.length - 1)
                    j = random.randint(0, self.height - 1)
                    if self.map[i][j] == 0:
                        break
                self.turn_pos1 = [i , j]
            if self.status == 1: # tanker be blocked when running to the 2nd turn
                while 1 :
                    i = random.randint(0, self.length - 1)
                    j = random.randint(0, self.length - 1)
                    if self.map[i][j] == 0:
                        break
                self.turn_pos2 = [i , j]   

    def search(self, map, start_position, end_position):
        """ init the algorithm, and find a route """
        self.reset()
        self.map = map
        self.length = len(map)
        self.height = len(map[0])

        self.adjust_turns(start_position)

        self.start_pos = start_position
        self.destination = end_position

        searchAl = astar.AStar()
        
        if self.status == 0: # start to run
            self.produce_turns()
            self.status = 1
        if self.status == 1: # run to the 1st turn
            self.reset_time -= 1
            if self.reset_time <= 0 :
                self.reset_time = 10
                self.status = 2
            if self.start_pos == self.turn_pos1: # reach the 1st turn
                self.status = 2
            else:
                self.way = searchAl.search(self.map, self.start_pos, self.turn_pos1)
        if self.status == 2: # run to the 2nd turn
            self.reset_time -= 1
            if self.reset_time <= 0 :
                self.reset_time = 10
                self.status = 0
            if self.start_pos == self.turn_pos2: # reach the 2nd turn
                self.status = 0
                temp_turns = []
                # random move for a little while, wait for next move
                while 1:
                    temp_turns = []
                    temp_turns.append(random.randint(0, self.length - 1))
                    temp_turns.append(random.randint(0, self.height - 1))
                    if self.map[temp_turns[0]][temp_turns[1]] == 0:
                        break
                self.way = searchAl.search(self.map, self.start_pos, temp_turns)
            else:
                self.way = searchAl.search(self.map, self.start_pos, self.turn_pos2)
        return self.way